<?php
class indexMod extends commonMod
{
	public function __construct()
	{
		parent::__construct();

	}
//显示管理后台首页
	public function index()
	{	
			global $config;
			$this->assign('config',$config);	
			$this->display('index/index');
	}
//登录页面
	public function login()
	{
		if(empty($_POST['do']))
		{
			$this->display('index/login');
			return;
		}
		//获取数据
		$username=in($_POST['username']);
		$password=md5($_POST['password']);

		//数据验证
		if(empty($username))
		{
			$this->error('请输入用户名');
		}
		if(empty($_POST['password']))
		{
			$this->error('请输入密码');		
		}
		if(empty($_POST['checkcode']))
		{
			$this->error('请输入验证码');
		}		
		if($_POST['checkcode']!=$_SESSION['verify'])
		{
			$this->error('验证码错误，请重新输入');
		}
		
		//数据库操作
		if($this->_login($username,$password))
		{
			$this->redirect(__APP__);
		}
		else
		{
			$this->error('用户名或密码错误，请重新输入');
		}	
	}
//用户登录
	private function _login($username,$password)
	{
		$condition=array();
		$condition['username']=$username;
		$user_info=$this->model->table('admin')->where($condition)->find();
		//用户名密码正确且没有锁定
		if(($user_info['password']==$password)&&($user_info['lock']==0))
		{			
			//更新帐号信息
			$data=array();			
			$data['login_time']=time();
			$data['login_ip']=get_client_ip();
			$this->model->table('admin')->data($data)->where($condition)->update();
			
				//设置登录信息
			$_SESSION['admin_uid']=$user_info['id'];
			$_SESSION['admin_groupid']=$user_info['groupid'];
			$_SESSION['admin_username']=$user_info['username'];
					
			return true;
		}	
		return false;
	}	
	
//用户退出
	public function logout()
	{
		unset($_SESSION['admin_uid']);
		unset($_SESSION['admin_username']);
		unset($_SESSION['admin_groupid']);
		$this->success('您已成功退出系统',__APP__);
	}
//生成验证码
	public function verify()
	{
		require_once(CP_PATH.'lib/Image.class.php');
		Image::buildImageVerify();
	}
/*
欢迎页面
*/
	public function welcome()
	{
		//$this->assign('user_info',$user_info);
		$this->display();
	}
}
?>